package com.lingyun.financialmanagement.utils;

import com.lingyun.financialmanagement.dao.mapper.BillMapper;
import com.lingyun.financialmanagement.dao.mapper.ComponentPartMapper;
import com.lingyun.financialmanagement.dao.mapper.ProjectMapper;
import com.lingyun.financialmanagement.dao.pojo.Bill;
import com.lingyun.financialmanagement.dao.pojo.BusinessTrip;
import com.lingyun.financialmanagement.dao.pojo.ComponentPart;
import com.lingyun.financialmanagement.dao.pojo.Project;
import com.lingyun.financialmanagement.vo.ErrorCode;

import java.math.BigDecimal;
import java.util.Objects;

public class ProjectUtils {
    // 增加材料成本   项目id， 增加金额
    public static void addWarehouseCost(Integer projectId, String methodsType, BigDecimal cost, ProjectMapper projectMapper, Integer billId, BillMapper billMapper) {

        // 获取当前税率
//        Bill bill = billMapper.selectById(billId);
//        BigDecimal taxRate = bill.getTaxRate();

        // 税后值 （暂时修改）
//        cost = cost.divide(new BigDecimal("1").add(taxRate),2,BigDecimal.ROUND_HALF_UP);

        // 根据公户或者现金付款调整
        Project project = projectMapper.selectById(projectId);

        if ("现金".equals(methodsType)) {
            project.setCashCost(project.getCashCost().add(cost));
        }
        else if ("公户".equals(methodsType)){
            project.setPublicCost(project.getPublicCost().add(cost));
        }
        else if ("公司赊销".equals(methodsType)){
            project.setCredit(project.getCredit().add(cost));
        }
        System.out.println("项目修改后信息" + project);
        projectMapper.updateById(project);
    }

    // 增加项目税额
    public static void addWarehouseTax(ComponentPart componentPart, ProjectMapper projectMapper, BillMapper billMapper) {
        Integer projectId = componentPart.getProject();
        Project project = projectMapper.selectById(projectId);
        // 计算税额
        // 获取税前总价 通过发票 计算税额
        BigDecimal costFront = componentPart.getCostFront();
        Integer billId = componentPart.getBill();
        Bill bill = billMapper.selectById(billId);
        BigDecimal taxRate = bill.getTaxRate();
        // 获取税额
//        Float tax = costFront - costFront / (1 + taxRate) ;
        BigDecimal tax = costFront.subtract(costFront.divide(taxRate.add(new BigDecimal(1)), 2, BigDecimal.ROUND_HALF_UP));
        if (tax.equals(new BigDecimal("0"))){
            return;
        }
        // 设置材料成本税额
        project.setPurchaseContractTax(project.getPurchaseContractTax().add(tax));

        projectMapper.updateById(project);
    }

    // 差旅增加项目税额
    public static void addWarehouseTax(BusinessTrip businessTrip, ProjectMapper projectMapper, BillMapper billMapper) {
        Integer projectId = businessTrip.getProject();
        Project project = projectMapper.selectById(projectId);
        // 计算税额
        // 获取税前总价 通过发票 计算税额
        BigDecimal costFront = businessTrip.getCost();
        Integer billId = businessTrip.getBill();
        Bill bill = billMapper.selectById(billId);
        BigDecimal taxRate = bill.getTaxRate();
        // 获取税额
//        Float tax = costFront - costFront / (1 + taxRate) ;
        BigDecimal tax = costFront.subtract(costFront).divide(taxRate.add(new BigDecimal(1)), 2, BigDecimal.ROUND_HALF_UP);
        if (tax.equals(new BigDecimal("0"))){
            return;
        }
        // 设置材料成本税额
        if (project.getType().equals("warehouse")){
            project.setPurchaseContractTax(project.getPurchaseContractTax().add(tax));
        }else {
            project.setPurchaseContractTax(project.getPurchaseContractTax().subtract(tax));
        }
        projectMapper.updateById(project);
    }

    // 减出当前元器件的金钱   (不扣税)
    public static void subtractComponents(ComponentPart componentPart, Boolean choose, Integer number, ProjectMapper projectMapper, BillMapper billMapper) {
        String paymentMethod = componentPart.getPaymentMethod();
        // 找到当前仓库
        Integer projectIdFront = componentPart.getProject();
        Project project = projectMapper.selectById(projectIdFront);
        // 获取税率
        Integer billId = componentPart.getBill();
        Bill bill = billMapper.selectById(billId);
        BigDecimal taxRate = bill.getTaxRate();
        // 元器件税后的值
//        float cost = componentPart.getCostFront() / (1 + taxRate);

        Boolean isTax = componentPart.getIsTax();

        BigDecimal cost;
        if (choose){
//            cost = componentPart.getCostFront().divide(taxRate.add(new BigDecimal(1)), 2, BigDecimal.ROUND_HALF_UP);
            // 如果未扣税就用税前金额即可，如果已扣税就用税后
            if (!isTax){
                cost = componentPart.getCostFront();
            }else{
                cost = componentPart.getCostFront().divide(taxRate.add(new BigDecimal(1)), 2, BigDecimal.ROUND_HALF_UP);
            }
        }
        // 如果不是全部取出，则要减去部分平分金钱值
        else {
//            cost = componentPart.getCostFront().divide(taxRate.add(new BigDecimal(1)), 2, BigDecimal.ROUND_HALF_UP).divide(new BigDecimal(componentPart.getNumber()), 2, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(number));
            // 如果未扣税就用税前金额即可，如果已扣税就用税后
            if (!isTax){
                cost = componentPart.getCostFront().divide(new BigDecimal(componentPart.getNumber()), 2, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(number));
            }else {
                cost = componentPart.getCostFront().divide(taxRate.add(new BigDecimal(1)), 2, BigDecimal.ROUND_HALF_UP).divide(new BigDecimal(componentPart.getNumber()), 2, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(number));
            }
        }

        System.out.println("扣除项目金额：" + cost);

        // 设置减去后的数值
        if (paymentMethod.equals("公户") || paymentMethod.equals("核销-公户")) {
            project.setPublicCost(project.getPublicCost().subtract(cost));
        }
        else if (paymentMethod.equals("现金") || paymentMethod.equals("核销-现金")){
            project.setCashCost(project.getCashCost().subtract(cost));
        }
        else if (paymentMethod.equals("公司赊销")){
            project.setCredit(project.getCredit().subtract(cost));
        }
        try{
            projectMapper.updateById(project);
        } catch (Exception e){
            throw new RuntimeException(ErrorCode.UPDATE_ERROR.getMsg());
        }
    }

    // 设置目标项目金额（去税后）
    public static void addProjectMoneyToTax(ComponentPart componentPart, Boolean choose, Integer number, ProjectMapper projectMapper, BillMapper billMapper, Integer projectId) {
        Project project = projectMapper.selectById(projectId);
        BigDecimal costFront = componentPart.getCostFront();
        Integer billId = componentPart.getBill();
        Bill bill = billMapper.selectById(billId);
        BigDecimal taxRate = bill.getTaxRate();
        // 获取税后本金
//        Float costTax = costFront / (1 + taxRate) ;
        String projectName = project.getName();

        BigDecimal costTax;
        if (choose){
            // 如果是公司就用税前的值，如果不是就用税后的值
            if (projectName.equals("公司")){
                costTax = costFront;
            }else{
                costTax = costFront.divide(taxRate.add(new BigDecimal(1)), 2, BigDecimal.ROUND_HALF_UP);
            }
        }else {
            // 如果是公司就用税前的值，如果不是就用税后的值
            if (projectName.equals("公司")){
                costTax = costFront.divide(BigDecimal.valueOf(componentPart.getNumber()),2, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(number));
            }else{
                costTax = costFront.divide(taxRate.add(new BigDecimal(1)), 2, BigDecimal.ROUND_HALF_UP).divide(BigDecimal.valueOf(componentPart.getNumber()),2, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(number));
            }
        }
        String paymentMethod = componentPart.getPaymentMethod();
        System.out.println("分开的金额" + costTax);
        if (paymentMethod.equals("公户") || paymentMethod.equals("核销-公户")) {
            project.setPublicCost(project.getPublicCost().add(costTax));
        }
        else if (paymentMethod.equals("现金") || paymentMethod.equals("核销-现金")){
            project.setCashCost(project.getCashCost().add(costTax));
        }
        else if (paymentMethod.equals("公司赊销")){
            project.setCredit(project.getCredit().add(costTax));
        }
        System.out.println(project);
        int update = 0;
        try{
            update = projectMapper.updateById(project);
        } catch (Exception e){
            throw new RuntimeException(ErrorCode.UPDATE_ERROR.getMsg());
        }
        if (update == 0){
            throw new RuntimeException(ErrorCode.UPDATE_ERROR.getMsg());
        }
    }

    // 减去当前元器件的金钱   所有 包括税
    public static void subtractComponents(ComponentPart componentPart, ProjectMapper projectMapper) {
        Integer projectId = componentPart.getProject();
        Project project = projectMapper.selectById(projectId);
        BigDecimal cost = componentPart.getCostFront();
        String paymentMethod = componentPart.getPaymentMethod();
        if ("公户".equals(paymentMethod) || "核销-公户".equals(paymentMethod)) {
            project.setPublicCost(project.getPublicCost().subtract(cost));
        }
        else if("现金".equals(paymentMethod) || "核销-现金".equals(paymentMethod)){
            project.setCashCost(project.getCashCost().subtract(cost));
        }
        else if ("公司赊销".equals(paymentMethod)){
            project.setCredit(project.getCredit().subtract(cost));
        }

        int update = 0;
        try{
            update = projectMapper.updateById(project);
        }catch (Exception e){
            throw new RuntimeException(ErrorCode.UPDATE_ERROR.getMsg());
        }
        if (update == 0){
            throw new RuntimeException(ErrorCode.UPDATE_ERROR.getMsg());
        }
    }

    // 减少当前元器件的税额   注：在哪个项目就减哪个项目的税
    public static void subtractWarehouseTax(ComponentPart componentPart, ProjectMapper projectMapper, BillMapper billMapper) {
        Integer projectId = componentPart.getProject();
        Integer billId = componentPart.getBill();
        Bill bill = billMapper.selectById(billId);
        Project project = projectMapper.selectById(projectId);
        BigDecimal taxRate = bill.getTaxRate();
        BigDecimal costFront = componentPart.getCostFront();

        // 获取税额
        BigDecimal tax = costFront.subtract(costFront.divide(taxRate.add(new BigDecimal(1)), 2, BigDecimal.ROUND_HALF_UP));
        if (tax.equals(new BigDecimal("0"))){
            return;
        }

        // 设置材料成本税额
        project.setPurchaseContractTax(project.getPurchaseContractTax().subtract(tax));
        int update = 0;
        try{
            update = projectMapper.updateById(project);
        } catch (Exception e){
            throw new RuntimeException(ErrorCode.UPDATE_ERROR.getMsg());
        }
        if (update == 0){
            throw new RuntimeException(ErrorCode.UPDATE_ERROR.getMsg());
        }
    }
}